#!/usr/bin/perl
#
# Do various user actions on an user entry.
#
use Counter;
use Counter::CGI;
use ErrorForm;
use POSIX;

$] > 5.008 && binmode(STDOUT, ":utf8"); # set web page output to utf-8
$q = new Counter::CGI;
# do business
$users = Counter::open(O_RDONLY);
$persons = FileTable::open("db/persons", O_RDONLY);
$key = $q->param('user');
$userrec = $users->get($key);
if (!$userrec) {
    errorForm("No user record for $key");
}

# Various actions that can happen. Because of the way multiple
# SUBMIT buttons work, this looks silly.
# The corresponding actions are named in www/adm/display-person.php
$user = $ENV{REMOTE_USER};
$date = strftime("%Y-%m-%d", localtime(time()));

if ($q->param("act.edit")) {
    print $q->redirect("/cgi-bin/adm/show-user.cgi?key=$key");
    exit(0);
   # note: MUST NOT fall through the end of the IF.
} elsif ($q->param("act.research")) {
   $$userrec{comment} = "$date: Being researched by $user";
   $userrec->store();
} elsif ($q->param("act.untraceable")) {
   $$userrec{comment} = "$date: Declared unfindable by $user";
   $userrec->store();
} elsif ($q->param("act.nolinux")) {
   $$userrec{comment} = "$date: $user says this is not a Linux user";
   $userrec->store();
} elsif ($q->param("act.drop")) {
   $$userrec{comment} = "$date: $user has stopped trying to find the user";
   $userrec->store();
} elsif ($q->param("act.freeze")) {
   $$userrec{comment} = "$date: Frozen manually by $user";
   $userrec->freeze();
} else {
   errorForm("Don't know what I'm supposed to do");
}
# If nothing else happens....
redirectback();

sub redirectback {
    print $q->redirect($q->referer());
}		 
